<template>
  <el-dialog
    :title="!dataForm.teamId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="团购默认图" prop="imgId">
      <el-input v-model="dataForm.imgId" placeholder="团购默认图"></el-input>
    </el-form-item>
    <el-form-item label="团购标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="团购标题"></el-input>
    </el-form-item>
    <el-form-item label="楼盘地址" prop="housesAddress">
      <el-input v-model="dataForm.housesAddress" placeholder="楼盘地址"></el-input>
    </el-form-item>
    <el-form-item label="开始日期" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="开始日期"></el-input>
    </el-form-item>
    <el-form-item label="结束日期" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="结束日期"></el-input>
    </el-form-item>
    <el-form-item label="团购介绍" prop="introduce">
      <el-input v-model="dataForm.introduce" placeholder="团购介绍"></el-input>
    </el-form-item>
    <el-form-item label="集合地址" prop="gatherAddress">
      <el-input v-model="dataForm.gatherAddress" placeholder="集合地址"></el-input>
    </el-form-item>
    <el-form-item label="车辆牌号" prop="carNum">
      <el-input v-model="dataForm.carNum" placeholder="车辆牌号"></el-input>
    </el-form-item>
    <el-form-item label="集合地点" prop="gatherTime">
      <el-input v-model="dataForm.gatherTime" placeholder="集合地点"></el-input>
    </el-form-item>
    <el-form-item label="参加团购人数" prop="buyCount">
      <el-input v-model="dataForm.buyCount" placeholder="参加团购人数"></el-input>
    </el-form-item>
    <el-form-item label="是否开启" prop="isOpen">
      <el-input v-model="dataForm.isOpen" placeholder="是否开启"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="是否修改" prop="delStatus">
      <el-input v-model="dataForm.delStatus" placeholder="是否修改"></el-input>
    </el-form-item>
    <el-form-item label="经纪人id" prop="brokerId">
      <el-input v-model="dataForm.brokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="团购房源名称" prop="houseName">
      <el-input v-model="dataForm.houseName" placeholder="团购房源名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          teamId: 0,
          imgId: '',
          title: '',
          housesAddress: '',
          startTime: '',
          endTime: '',
          introduce: '',
          gatherAddress: '',
          carNum: '',
          gatherTime: '',
          buyCount: '',
          isOpen: '',
          createTime: '',
          updateTime: '',
          delStatus: '',
          brokerId: '',
          houseName: ''
        },
        dataRule: {
          imgId: [
            { required: true, message: '团购默认图不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '团购标题不能为空', trigger: 'blur' }
          ],
          housesAddress: [
            { required: true, message: '楼盘地址不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始日期不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束日期不能为空', trigger: 'blur' }
          ],
          introduce: [
            { required: true, message: '团购介绍不能为空', trigger: 'blur' }
          ],
          gatherAddress: [
            { required: true, message: '集合地址不能为空', trigger: 'blur' }
          ],
          carNum: [
            { required: true, message: '车辆牌号不能为空', trigger: 'blur' }
          ],
          gatherTime: [
            { required: true, message: '集合地点不能为空', trigger: 'blur' }
          ],
          buyCount: [
            { required: true, message: '参加团购人数不能为空', trigger: 'blur' }
          ],
          isOpen: [
            { required: true, message: '是否开启不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delStatus: [
            { required: true, message: '是否修改不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          houseName: [
            { required: true, message: '团购房源名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.teamId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.teamId) {
            this.$http({
              url: this.$http.adornUrl(`/broker/teambuy/info/${this.dataForm.teamId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.imgId = data.teamBuy.imgId
                this.dataForm.title = data.teamBuy.title
                this.dataForm.housesAddress = data.teamBuy.housesAddress
                this.dataForm.startTime = data.teamBuy.startTime
                this.dataForm.endTime = data.teamBuy.endTime
                this.dataForm.introduce = data.teamBuy.introduce
                this.dataForm.gatherAddress = data.teamBuy.gatherAddress
                this.dataForm.carNum = data.teamBuy.carNum
                this.dataForm.gatherTime = data.teamBuy.gatherTime
                this.dataForm.buyCount = data.teamBuy.buyCount
                this.dataForm.isOpen = data.teamBuy.isOpen
                this.dataForm.createTime = data.teamBuy.createTime
                this.dataForm.updateTime = data.teamBuy.updateTime
                this.dataForm.delStatus = data.teamBuy.delStatus
                this.dataForm.brokerId = data.teamBuy.brokerId
                this.dataForm.houseName = data.teamBuy.houseName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/broker/teambuy/${!this.dataForm.teamId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'teamId': this.dataForm.teamId || undefined,
                'imgId': this.dataForm.imgId,
                'title': this.dataForm.title,
                'housesAddress': this.dataForm.housesAddress,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'introduce': this.dataForm.introduce,
                'gatherAddress': this.dataForm.gatherAddress,
                'carNum': this.dataForm.carNum,
                'gatherTime': this.dataForm.gatherTime,
                'buyCount': this.dataForm.buyCount,
                'isOpen': this.dataForm.isOpen,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'delStatus': this.dataForm.delStatus,
                'brokerId': this.dataForm.brokerId,
                'houseName': this.dataForm.houseName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
